// JavaScript Document
(function (window, $) {

    // var login = {
    //
    //
    //     pwdLogin: function () {
    //         var form = $('#pwdLoginForm');
    //         var arrParams = form.serializeArray();
    //         var data = {};
    //         $(arrParams).each(function (index, item) {
    //             data[item.name] = item.value;
    //         });
    //         data.userPwd = hex_md5(data.userPwd);
    //         console.log('loginParams: ', data);
    //
    //         $.ajax({
    //             url: 'user/login',
    //             method: 'post',
    //             data: data,
    //             success: function (result) {
    //                 if (result.code === 'SUCCESS') {
    //                     location.href = 'index?sid=' + result.data.sid;
    //                 } else {
    //                     alert(result.msg);
    //                 }
    //             },
    //             error: function (err) {
    //                 console.log('error message: ', err);
    //             }
    //         })
    //
    //     }
    // };

    $(function () {
        var w_w = $(window).width(), w_h = $(window).height();
        var bili = 1084 / 1920, biliCur = 1;


        winInite();

        $(window).resize(function () {
            winInite();
        });


        function winInite() {
            w_w = $(window).width(), w_h = $(window).height();
            //$(".welcome").css({width:w_w,height:w_h});
            //document.title="w_w:"+w_w+" || height:"+w_h;
            //console.log("w_w:"+w_w+" || height:"+w_h);
            biliCur = w_h / w_w;

            if (biliCur > bili) {
                $(".welImg").css({
                    height: w_h,
                    width: 'auto',
                    'margin-left': -(w_w / 2),
                    'margin-top': -(w_h / 2)
                });
            }

            if (biliCur <= bili) {
                $(".welImg").css({
                    width: w_w,
                    height: 'auto',
                    'margin-left': -(w_w / 2),
                    'margin-top': -(w_h / 2)
                });
            }
        }

        // $('.loginType').on('click', function () {
        //     var obj = $(this);
        //     if (!obj.hasClass('active')) {
        //         obj.find('ul').stop(true, true).slideDown(200);
        //         obj.addClass('active');
        //     } else {
        //         obj.find('ul').stop(true, true).slideUp(50);
        //         obj.removeClass('active');
        //     }
        // });

        // 切换登录方式
        // $('.loginType').on('click', '.type-menu li', function (ev) {
        //     var target = $(this);
        //     var authType = target.attr('type');
        //     var index = target.index() || 0;
        //     var forgetPwdItem = $('.forgetpwd');
        //     var current = $('.loginType b');
        //     var authTypeItem = $('#authType');
        //
        //     var containers = $('.loginBox');
        //     containers.removeClass('active');
        //
        //     $('.loginType').removeClass('active');
        //     $('.loginType ul').stop(true, true).slideUp(0);
        //     if (index == 0) {
        //         forgetPwdItem.show();
        //     } else {
        //         forgetPwdItem.hide();
        //     }
        //
        //     containers.eq(index).addClass('active');
        //     current.html(target.text());
        //     authTypeItem.val(authType);
        //
        //
        //     return false;
        // });

        // pwd login
        // $('#pwdLoginForm').on('submit', function (ev) {
        //     ev.preventDefault();
        //     login.pwdLogin();
        // });


    });

    // <%--认证方式：1-人脸识别,2-短信验证,3-指纹，4-密码--%>
    var loginModel = [
    	    {type: "1", name: "人脸识别"},
            {type: "4", name: "密码登录"},
            {type: "2", name: "验证码登录(暂未开放)", disabled: true},
            
            {type: "3", name: "指纹登录"}
        ];

    var vm = avalon.define({
        $id: 'avalonLogin',
        loginModel: loginModel,
        current: loginModel[window.authType || 0],
        showMenu: false,
        account: window.localStorage.getItem('account') || '',
        pwd: '',
        serverErrMsg: '',
        rememberAccount: false,
        errMsg: '',
        changeMenu: function (el) {
            if (!el.disabled) {
                vm.current = el.$model;
                vm.showMenu = false;
            }
        },
        enterSubmit: function (e) {
            if (e.keyCode === 13) {
                vm.validate.onManual();
            }
        },
        mouseleave:function(e){
        	console.log('info:', "开始获取用户认证头像");
        	if (!vm.account) {
                vm.showErrMsg = true;
                return;
            }
        	
        	vm.getUserDectHeadBase64Img();
        	OpenVideo();
        	vm.faceLogin();
        	
        },
        
        faceLogin : function(){
        	
        	if (!vm.account) {
                vm.showErrMsg = true;
                return;
            }
        	
        	var ret = VerifyFaceDetect();
        	if(ret == -2) {
        		return ;
        	}
        	var radio = parseInt(faceRadio); 
        	if(ret < radio){
        		
        		window.setTimeout(function () {
    				vm.faceLogin();
                }, 2000);
        		console.log('人脸比对值为:', ret + ", 重新比对");
        		return ;
        	}
        	
            var data = {
                account: vm.account,
                similar: ret,
                authType: vm.current.type
            };
            $.ajax({
                url: 'user/login',
                method: 'post',
                data: data,
                dataType:"json",
                success: function (result) {
                    if (result.code === 'SUCCESS') {
                        // remember account
                        if (vm.rememberAccount) {
                            window.localStorage.setItem('account', vm.account);
                        } else {
                            window.localStorage.removeItem('account');
                        }
                        location.href = 'index?sid=' + result.data.sid;
                    } else {
                        alert(result.msg);
                        vm.errMsg = result.msg;
                    }
                },
                error: function (err) {
                   // console.log('error message: ', err);
                }
            })
        },
        
        getUserDectHeadBase64Img : function(){
        	
	       	 $.ajax({  
	   		        type : "POST",  //提交方式  
	   		        url : "user/face/img?account=" + vm.account,//路径
	   		        async : false,
	   		        data:null,
	   		        dataType:"json",
	   		        success : function(result) {//返回数据根据结果进行相应的处理  
	   		        	$("#certImg").val(result.data);
	   		        }  
	   		 });
       	
        },
        
        login: function (ev) {
            if (!vm.account || !vm.pwd) {
                vm.showErrMsg = true;
                return;
            }

            var data = {
                account: vm.account,
                userPwd: hex_md5(vm.pwd),
                authType: vm.current.type
            };
            $.ajax({
                url: 'user/login',
                method: 'post',
                data: data,
                dataType:"json",
                success: function (result) {
                    if (result.code === 'SUCCESS') {
                        // remember account
                        if (vm.rememberAccount) {
                            window.localStorage.setItem('account', vm.account);
                        } else {
                            window.localStorage.removeItem('account');
                        }
                        location.href = 'index?sid=' + result.data.sid;
                    } else {
                        // alert(result.msg);
                        vm.errMsg = result.msg;
                    }
                },
                error: function (err) {
                   // console.log('error message: ', err);
                }
            })
        },
        showError: function (reasons) {
            if (reasons.length) {
                vm.errMsg = reasons[0].message;
            }
        },
        validate: {
            onValidateAll: function(reasons){
                vm.showError(reasons);
                if (!reasons.length) {
                    vm.login();
                }
            },
            onManual: function(){},
            onError: function (reasons, ev) {
                vm.showError(reasons);
            },
            onSuccess: function (reasons, ev) {
                vm.errMsg = '';
            },
            // onComplete: avalon.noop,//针对单个表单元素
            // onReset: avalon.noop, //针对单个表单元素
            // validateInBlur: true, // {Boolean} true，在blur事件中进行验证,触发onSuccess, onError, onComplete回调
            // validateInKeyup: true, // {Boolean} true，在keyup事件中进行验证,触发onSuccess, onError, onComplete回调
            validateAllInSubmit: false // {Boolean} true，在submit事件中执行onValidateAll回调
            // resetInFocus: true, // {Boolean} true，在focus事件中执行onReset回调,
            // deduplicateInValidateAll: false // {Boolean} false，在validateAll回调中对reason数组根据元素节点进行去重
        }
        
    });
   
}(window, $));
